home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_100 / 101_01 / iquiz.c < prev    next >
Text File  |  1985-11-13  |  8KB  |  272 lines

  1.  
  2. /**********************************************************
  3.  ***                            ***
  4.  ***    Copyright (c) 1981 by David M. Fogg        ***
  5.  ***                            ***
  6.  ***        2632 N.E. Fremont            ***
  7.  ***        Portland, OR 97212            ***
  8.  ***                            ***
  9.  ***        (503) 288-3502{HM} || 223-8033{WK}        ***
  10.  ***                            ***
  11.  ***    Permission is herewith granted for non-     ***
  12.  ***    commercial distribution through the BDS C    ***
  13.  ***    User's Group; any and all forms of commercial   ***
  14.  ***    redistribution are strenuously unwished-for.    ***
  15.  ***                            ***
  16.  **********************************************************/
  17.  
  18. /* ---> IQUIZ <--- : UCSD Pascal -> BDS C conversion
  19.  
  20.    24 Nov 80 - creation day
  21.    29 Nov: more creation
  22.    30 Nov: final touches
  23.  
  24. This version differs from the Pascal one in that the entire
  25. question file is read into memory - which means: 1) that the
  26. question file is NOT a random-access file, and 2) that
  27. file maintenance is done with a standard editor, rather than
  28. in this program.
  29.  
  30.       Original & adaptation conceived & coded by:
  31.       David M. Fogg - Portland, Oregon
  32.  
  33.      COPYRIGHT (C) 1980, DAVID M. FOGG
  34.  
  35. */
  36.  
  37. #include <std.h>
  38.  
  39. #define  KBPORT   17    /* Keyboard Data Port *** SYSTEM DEPENDENCY ***/
  40.  
  41. #define  YQBAS      11    /* y-ord of 1st maybe */
  42. #define  STARCL   100    /* starting klok kount */
  43. #define  QLINES   3
  44. #define  MAYBES   4
  45. #define  MAXQ      333    /* maximum # of questions */
  46. #define  KBDATA   17    /* Keyboard Data Port *** SYSTEM-DEPENDENT ***/
  47. #define  FUGIT      3000    /* default clock tarditude factor */
  48. #define  ROUND      10    /* default questions/round */
  49.  
  50. /*
  51.    # # # # #  GLOBALS  # # # # #
  52. */
  53. char *qline[MAXQ], *maybe[MAXQ];
  54. char *curmeb[MAYBES];       /* current answers in shuffled order */
  55. int numrex;           /* # records in memory */
  56. int asked;           /* # asked so far (quit when == numrex) */
  57. BYTE answer;           /* screen loc of answer */
  58. int rec;           /* current question from file */
  59. int order[MAYBES];       /* screen order of possible answers */
  60. int tempus;
  61. char higuy[20], loboy[20];
  62. int hiscore, loscore;
  63. char player[20];       /* hi/low scorers, curr player */
  64. int score;
  65. int fugit;
  66.  
  67.  
  68. main (ac, av)
  69. int ac;
  70. char *av[];
  71. {
  72.    BYTE round;              /* # questions in a round */
  73.    BYTE qnum;              /* question # in round */
  74.    BOOL missed;           /* whether s/he's missed once already */
  75.    BYTE kount;              /* kurrent klok kount */
  76.    BYTE guess;              /* current guess */
  77.    char olgess;           /* prev guess (to tell if he hit a key) */
  78.    char filnam[MAXNAM];
  79.    BOOL noteof;
  80.    char ibuf[BUFSIZ];
  81.    char *memtop;          /* hiest avail memloc 4 Q/A lns */
  82.    char *mp;              /* Mem Ptr used while loading Q/A lns */
  83.    BYTE i;
  84.    int l;
  85.  
  86.    putchar(' ');    /* force loading of local version */
  87.  
  88.    round = ROUND; fugit = FUGIT;
  89.    noteof = YES;
  90.    asked = numrex = 0;
  91.    loscore = 9999; hiscore = -1;
  92.    memtop = topofmem() - 1000;
  93.    mp = sbrk(1);
  94.  
  95. /*
  96.    ---> DETERMINE/OPEN/READ QUESTION/ANSWER FILE <---
  97. */
  98.    if (ac < 2)
  99.       strcpy(filnam, "iquiz.dat");
  100.    else
  101.       strcpy(filnam, av[1]);
  102.    if (fopen(filnam, ibuf) == ERROR)
  103.       errxit("Can't open file");
  104.  
  105.    while (noteof && numrex < MAXQ && mp < memtop)
  106.       for (i = 0; i < QLINES+MAYBES; ++i) {
  107.      if (fgets(mp, ibuf) == 0) {
  108.         noteof = NO;
  109.         break;
  110.      }
  111.      if (i == 0) qline[numrex] = mp;
  112.      if (i == QLINES) maybe[numrex++] = mp;
  113.      l = strlen(mp);
  114.      mp[l-1] = NULL;
  115.      mp += l;
  116.       }
  117.  
  118.    printf("\nExtra Memory: %u", memtop - mp);
  119.    printf("\n%d questions\n\n", numrex);
  120.    puts("Questions/round(default: 10) - "); scanf("%d", &round);
  121.    if (round < 1) round = ROUND;
  122.    puts("Clock delay rate (default: 3000) - "); scanf("%d", &fugit);
  123.    if (fugit < 1) fugit = FUGIT;
  124.    nrand(0, "Hit return someday..."); getchar();
  125.    puts(CLEAR);
  126.  
  127. /*
  128.    >>>--------->  MAIN LOOP  <---------<<<
  129. */
  130.    do {
  131.       puts("Player's name, or 'Q'(uit) - "); gets(player);
  132.       if (toupper(player[0]) == 'Q')
  133.      break;
  134.       puts("RETURN to start..."); getchar();
  135.       score = 0; qnum = round;
  136.       do {
  137.      puts(CLEAR); puts(CUROFF);
  138.      toxy(30,1); printf("< QUESTION # %d >", qnum);
  139.      toxy(5,3); printf("Score: %d", score);
  140.      toxy(50,3); puts("Count --->");
  141.      showq(rec = getq());
  142.      missed = NO; kount = STARCL; guess = -1;
  143.      do {
  144.         toxy(63,3); puts(VIDINV); printf("%3d ", kount); puts(VIDNOR);
  145.         guess = (inp(KBPORT) & 127) - '1';
  146.         if (guess < 0 || guess >= MAYBES) olgess = guess;
  147.         if (guess != answer && olgess != guess) {
  148.            hilite(3, guess * 2 + YQBAS, "{NO}");
  149.            olgess = guess;
  150.            if (missed)
  151.           kount = 1;
  152.            else {
  153.           kount = kount / 2 + 1;
  154.           missed = YES;
  155.            }
  156.         }
  157.         tempus = fugit; while (--tempus);
  158.      } while (--kount && guess != answer);
  159.      if (kount) ++kount;
  160.      score += kount;
  161.      toxy(5,3); printf("Score: %d", score);
  162.      toxy(63,3); puts(VIDINV); printf("%3d ", kount); puts(VIDNOR);
  163.      if (kount) {
  164.         hilite(3, answer * 2 + YQBAS, "YES!");
  165.      }
  166.      hilite(13, answer * 2 + YQBAS, maybe[rec]);
  167.      puts(CURON);
  168.      if (!getyn(10, 21, "Continue"))
  169.         qnum = 1;
  170.      toxy(10, 21);
  171.      if (kbhit()) getchar();
  172.      qline[rec] = NULL;
  173.      ++asked;
  174.       } while (--qnum);
  175.       shostat();
  176.    } while (asked < numrex-round && toupper(player[0]) != 'Q');
  177.    if (round > numrex-asked) {
  178.       puts("\n\nToo few questions for a new round: RETURN to exit - ");
  179.       getchar();
  180.    }
  181.    puts(POCRT);
  182. }
  183.  
  184. /*
  185.    =========< SUBROUTINES >=========
  186. */
  187.  
  188. hilite (x, y, msg)       /* ---<PRINT msg IN INVIDEO>--- */
  189. BYTE x, y;
  190. char *msg;
  191. {
  192.    toxy(x, y); printf("%s%s%s", VIDINV, msg, VIDNOR);
  193. }
  194.  
  195.  
  196. getq ()            /* ---<RETURN RANDOM QUESTION #>--- */
  197. {
  198.    int r;
  199.  
  200.    r = nrand(1) % numrex;
  201.  
  202.    while (qline[r] == NULL) r = (r + 1) % numrex;
  203.  
  204.    return (r);
  205. }
  206.  
  207.  
  208. showq (qno)           /* ---<SHOW QUESTION & MAYBES>--- */
  209. int qno;
  210. {
  211.    BYTE i, t;
  212.    char *l;
  213.    int qlen;
  214.    char anset[10];
  215.  
  216.    qlen = 0;
  217.    strcpy(anset, "0123");
  218.  
  219.    for (i = 0; i < MAYBES; ++i) {      /* shuffle answers */
  220.       t = nrand(1) % MAYBES;
  221.       while (inset(anset, t + '0') == 0) t = (t+1) % MAYBES;
  222.       order[i] = t;
  223.       anset[t] = 'X';
  224.    }
  225.    answer = order[0];
  226.  
  227.    l = maybe[qno];            /* retrieve answers in shuffled order */
  228.    for (i = 0; i < MAYBES; ++i) {
  229.       curmeb[order[i]] = l;
  230.       l += strlen(l) + 1;
  231.    }
  232.  
  233.    l = qline[qno];          /* display question & answers */
  234.    for (i = 0; i < QLINES; ++i) {
  235.       toxy(5, 6 + i); puts(l);
  236.       qlen += strlen(l);
  237.       l += strlen(l) + 1;
  238.    }
  239.    for (i = 0; i < MAYBES; ++i) {
  240.       toxy(9, i * 2 + YQBAS);
  241.       printf("%d : %s", i + 1, curmeb[i]);
  242.    }
  243.  
  244.    tempus = fugit * 9 + qlen * 99;
  245.    while (--tempus);    /* givum time to read it b4 starting the clock */
  246. }
  247.  
  248.  
  249. shostat ()         /* ---<DISPLAY STANDINGS>--- */
  250. {
  251.    toxy(10,21); puts("RETURN to see standings - "); getchar();
  252.  
  253.    if (score > hiscore) {
  254.       hiscore = score; strcpy(higuy, player);
  255.    }
  256.    if (score < loscore) {
  257.       loscore = score; strcpy(loboy, player);
  258.    }
  259.  
  260.    puts(CLEAR);
  261.    printf("High scorer: %-20s   {%4d }\n\n", higuy, hiscore);
  262.    printf("Curr player: %-20s   {%4d }\n\n", player, score);
  263.    printf(" Low scorer: %-20s   {%4d }\n\n", loboy, loscore);
  264.    printf("QUESTIONS - used: %d   left: %d\n\n", asked, numrex - asked);
  265. }
  266.  
  267. putchar(c)      /* ---<MAKE putchar() BE putch()>--- */
  268. char c;
  269. {
  270.    putch(c);
  271. }
  272.